package com.ruoyi.biz.plan.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.biz.plan.enums.PlanType;
import com.ruoyi.biz.plan.enums.SourceType;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.plan.mapper.BizPlanConfigMapper;
import com.ruoyi.biz.plan.domain.BizPlanConfig;
import com.ruoyi.biz.plan.service.IBizPlanConfigService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 计划配置Service业务层处理
 *
 * @author Fei.Cong
 * @date 2024-07-13
 */
@Service
public class BizPlanConfigServiceImpl extends ServiceImpl<BizPlanConfigMapper, BizPlanConfig> implements IBizPlanConfigService {

    @Autowired
    private BizPlanConfigMapper bizPlanConfigMapper;

    /**
     * 分页查询计划配置
     *
     * @param page
     * @param bizPlanConfig 计划配置
     */
    @Override
    public IPage<BizPlanConfig> pageList(BizPlanConfig bizPlanConfig, IPage<BizPlanConfig> page) {
        return bizPlanConfigMapper.selectPageList(page, bizPlanConfig);
    }

    /**
     * 条件查询计划配置
     *
     * @param bizPlanConfig 计划配置
     * @return
     */
    @Override
    public List<BizPlanConfig> queryList(BizPlanConfig bizPlanConfig) {
        LambdaQueryWrapper<BizPlanConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return bizPlanConfigMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void checkPlan(BizPlanConfig bizPlanConfig) {
        StringBuilder msg = new StringBuilder();
        for (String billMonth : bizPlanConfig.getBillMonths()) {
            BizPlanConfig planConfig = this.getOne(new LambdaQueryWrapper<BizPlanConfig>()
                    .eq(BizPlanConfig::getBillMonth, billMonth)
                    .eq(StrUtil.isNotBlank(bizPlanConfig.getSource()), BizPlanConfig::getSource, bizPlanConfig.getSource())
                    .eq(StrUtil.isNotBlank(bizPlanConfig.getType()), BizPlanConfig::getType, bizPlanConfig.getType())
                    .eq(BizPlanConfig::getStatus, UserStatus.OK.getCode()));

            Validator.validateNotNull(planConfig, String.format("当前%s月台账，未配置计划，请联系管理员", billMonth));

            if (!(DateUtil.compare(new Date(), planConfig.getBeginDate()) >= 0 && DateUtil.compare(new Date(), planConfig.getEndDate()) <= 0)) {
                msg.append(String.format("%s月台账，允许操作窗口期%s~%s",
                        planConfig.getBillMonth(),
                        DateUtil.format(planConfig.getBeginDate(), "yyyy-MM-dd"),
                        DateUtil.format(planConfig.getEndDate(), "yyyy-MM-dd"))).append("<br/>");
            }
        }
        if (StrUtil.isNotBlank(msg.toString())) {
            msg.insert(0, "导入文件，存在异常数据<br/>").append("请检查导入文件！");
            throw new ServiceException(msg.toString());
        }
    }

}
